#region Using
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using CY.GFive.Core.Business;
using CY.Utility.DBUtility;
#endregion Using

namespace CY.GFive.DALProviders.SqlServerProvider
{
	public class UserRoleProvider : CY.GFive.Core.Providers.DALProvider.IUserRoleProvider
	{
		#region Sql
		private static readonly string SqlSelectUserRole = "SELECT [Id], [RoleKey],[UserKey],[UserType] FROM [UserRole] WHERE  [Id]=@Id";

		private static readonly string SqlInsertUserRole = "INSERT INTO [UserRole]([RoleKey],[UserKey],[UserType]) values(@RoleKey,@UserKey,@UserType);SELECT SCOPE_IDENTITY();";

		private static readonly string SqlUpdateUserRole = "UPDATE [UserRole] SET [RoleKey] = @RoleKey,[UserKey] = @UserKey,[UserType] = @UserType WHERE  [Id]=@Id";

		private static readonly string SqlDeleteUserRole = "DELETE [UserRole] WHERE  [Id]=@Id";

		private static readonly string SqlGetAllUserRole = "SELECT [Id], [RoleKey],[UserKey],[UserType] FROM [UserRole]";
		#endregion

		#region IUserRoleProvider Members
		public Core.Business.UserRole Select(int id)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, id);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlSelectUserRole);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				Core.Business.UserRole userRole = new Core.Business.UserRole();

				if (!reader.IsDBNull(0)) userRole.Id = reader.GetInt32(0);
				if (!reader.IsDBNull(1)) userRole.RoleKey = reader.GetInt32(1);
				if (!reader.IsDBNull(2)) userRole.UserKey = reader.GetInt32(2);
				if (!reader.IsDBNull(3)) userRole.UserType = reader.GetInt32(3);

				return userRole;
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();

				return null;
			}
		}

		public void Insert(Core.Business.UserRole userRole)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@RoleKey", SqlDbType.Int, userRole.RoleKey);
			sql.AddParameter("@UserKey", SqlDbType.Int, userRole.UserKey);
			sql.AddParameter("@UserType", SqlDbType.Int, userRole.UserType);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlInsertUserRole);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				if (!reader.IsDBNull(0)) userRole.Id = int.Parse(reader.GetDecimal(0).ToString());

				reader.Close();
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();
			}
		}

		public void Update(Core.Business.UserRole userRole)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@RoleKey", SqlDbType.Int, userRole.RoleKey);
			sql.AddParameter("@UserKey", SqlDbType.Int, userRole.UserKey);
			sql.AddParameter("@UserType", SqlDbType.Int, userRole.UserType);
			sql.AddParameter("@Id", SqlDbType.Int, userRole.Id);
			sql.ExecuteSql(SqlUpdateUserRole);
		}

		public void Delete(Core.Business.UserRole userRole)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, userRole.Id);
			sql.ExecuteSql(SqlDeleteUserRole);
		}

		public IList<Core.Business.UserRole> GetAllUserRole()
		{
			IList<Core.Business.UserRole> userRolelist = new List<Core.Business.UserRole>();
			SqlServerUtility sql = new SqlServerUtility();

			SqlDataReader reader = sql.ExecuteSqlReader(SqlGetAllUserRole);

			if(reader != null)
			{
				while(reader.Read())
				{
					Core.Business.UserRole userRole = new Core.Business.UserRole();

					if (!reader.IsDBNull(0)) userRole.Id = reader.GetInt32(0);
					if (!reader.IsDBNull(1)) userRole.RoleKey = reader.GetInt32(1);
					if (!reader.IsDBNull(2)) userRole.UserKey = reader.GetInt32(2);
					if (!reader.IsDBNull(3)) userRole.UserType = reader.GetInt32(3);

					userRole.MarkOld();
					userRolelist.Add(userRole);
				}
				reader.Close();
			}
			return userRolelist;
		}
		#endregion
	}
}
